package com.ljm.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.ljm.servlect.OrderServiceImpl3;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.awt.windows.ThemeReader;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {

    @Autowired
    OrderServiceImpl3 orderServiceImpl3;

    @GetMapping("/testA")
    public String testA(){
        //// 控制使用线程
//        try{
//            TimeUnit.MICROSECONDS.sleep(900);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        // 排队等待  时间输出
        //log.info(Thread.currentThread().getName()+"\t"+"============ testA");
        this.orderServiceImpl3.dosomething();
        return "-----------------testA";
    }

    @GetMapping("/testB")
    public String testB(){
        //log.info(Thread.currentThread().getName()+"\t"+"============ testA");
        this.orderServiceImpl3.dosomething();
        return "-----------------testB";
    }
    /*     Sentinel  测试降级规则使用     */

    @GetMapping("/testD")
    public String testD(){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     //   int a =10/0;
        log.info("testD 测试RT");
        return "-----------------testD";
    }

    /*       Sentinel  热点规则 使用           */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2){

        return "--------------testHotKey";
    }
    public String deal_testHotKey(String p1, String p2, BlockException exception){
        return "------------当前人山人海，请稍后再试！！！  o(╥﹏╥)o";
    }

}
